package com.jeecms.cms.dao.main.impl;

import com.jeecms.cms.dao.main.BranchDao;
import com.jeecms.cms.entity.assist.Branch;
import com.jeecms.common.hibernate4.Finder;
import com.jeecms.common.hibernate4.HibernateBaseDao;
import com.jeecms.common.page.Pagination;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class BranchDaoImpl extends HibernateBaseDao<Branch, Integer>
        implements BranchDao {
    @Override
    public List<Branch> getTopList(Integer siteId, boolean hasEnterpriseOnly, boolean cacheable) {
        Finder f = getTopFinder(siteId, hasEnterpriseOnly, cacheable);
        return find(f);
    }

    @Override
    public List<Branch> getTopListByRigth(Integer userId, Integer siteId, boolean hasEnterpriseOnly, boolean cacheable) {
        return null;
    }

    @Override
    public List<Branch> getTopListForTag(Integer siteId, boolean hasEnterpriseOnly, boolean cacheable) {
        return null;
    }

    @Override
    public Pagination getTopPageForTag(Integer siteId, boolean hasEnterpriseOnly, int pageNo, int pageSize) {
        Finder f = Finder.create("from Branch bean");
        f.append(" where bean.site.id=:siteId and bean.parent is null");
        f.setParam("siteId", siteId);
        if (hasEnterpriseOnly) {
            //f.append(" and bean.hasEnterprise=true");
        }
        f.append(" order by bean.priority asc,bean.id asc");
        f.setCacheable(true);
        return find(f, pageNo, pageSize);
    }

    @Override
    public List<Branch> getChildList(Integer parentId, boolean hasEnterpriseOnly, boolean cacheable) {
        Finder f = getChildFinder(parentId, hasEnterpriseOnly, cacheable);
        return find(f);
    }

    @Override
    public List<Branch> getChildListByRight(Integer userId, Integer siteId, Integer parentId, boolean hasEnterpriseOnly, boolean cacheable) {
        Finder f = Finder.create("select bean from Branch bean");
        f.append(" join bean.users user");
        f.append(" where user.id=:userId and bean.parent.id=:parentId");
        f.setParam("userId", userId).setParam("parentId", parentId);
        if (hasEnterpriseOnly) {
            //f.append(" and bean.hasContent=true");
        }
        f.append(" order by bean.priority asc,bean.id asc");
        f.setCacheable(true);
        return find(f);
    }

    @Override
    public List<Branch> getChildListForTag(Integer parentId, boolean hasEnterpriseOnly, boolean cacheable) {
        return null;
    }

    @Override
    public List<Branch> getBottomList(Integer siteId, boolean hasEnterpriseOnly, boolean cacheable) {
        return null;
    }

    @Override
    public Pagination getChildPageForTag(Integer parentId, boolean hasEnterpriseOnly, int pageNo, int pageSize) {
        Finder f = Finder.create("from Branch bean");
        f.append(" where bean.parent.id=:parentId");
        f.setParam("parentId", parentId);
        if (hasEnterpriseOnly) {
            //f.append(" and bean.hasContent=true");
        }

        f.append(" order by bean.priority asc,bean.id asc");
        f.setCacheable(true);
        return find(f, pageNo, pageSize);
    }

    @Override
    public Branch findById(Integer id) {
        Branch entity = get(id);
        return entity;
    }

    @Override
    public Branch save(Branch bean) {
        getSession().save(bean);
        return bean;
    }

    @Override
    public Branch copy(Integer bid, Integer siteId) {
        return null;
    }

    @Override
    public Branch update(Branch bean) {
        return null;
    }

    @Override
    public Branch deleteById(Integer id) {
        Branch entity = super.get(id);
        if (entity != null) {
            try {
                getSession().delete(entity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return entity;
    }

    @Override
    public Branch[] updatePriority(Integer[] ids, Integer[] priority) {
        return new Branch[0];
    }

    @Override
    public void initWorkFlow(Integer workflowId) {

    }

    @Override
    public String checkDelete(Integer id) {
        Finder f = getChildFinder(id, true, true);
        return String.valueOf(countQueryResult(f));
    }

    @Override
    protected Class<Branch> getEntityClass() {
        return Branch.class;
    }

    private Finder getChildFinder(Integer parentId, boolean hasEnterpriseOnly,  boolean cacheable) {
        Finder f = Finder.create("from Branch bean");
        f.append(" where bean.parent.id=:parentId");
        f.setParam("parentId", parentId);
        if (hasEnterpriseOnly) {
            //f.append(" and bean.hasEnterprise=true");
        }

        f.append(" order by bean.priority asc,bean.id asc");
        f.setCacheable(cacheable);
        return f;
    }

    private Finder getTopFinder(Integer siteId, boolean hasEnterpriseOnly,  boolean cacheable) {
        Finder f = Finder.create("from Branch bean");
        f.append(" where bean.site.id=:siteId and bean.parent is null");
        f.setParam("siteId", siteId);
        if (hasEnterpriseOnly) {
            //f.append(" and bean.hasEnterprise=true");
        }

        f.append(" order by bean.priority asc,bean.id asc");
        f.setCacheable(cacheable);
        return f;
    }
}
